package com.customization.assets.util;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;

/**
 * @author	Huangwm
 * @description	实物资产管理应用中使用的后台数据库操作方法
 */
public class DBTools {
	private LogTool log = new LogTool("/log/assetlog/huangwm/DBTools",false);
	/**
	 * 根据用户id获取部门id
	 * @param userid
	 * @return departmentid
	 */
	public  String getDepid(String userid){
		if(userid.equals("") || userid.equals("1")){
			return "0";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select departmentid from hrmresource where id="+userid);
		log.writeLog("getDepid---> select departmentid from hrmresource where id="+userid);
		if(rs.next()){
			return rs.getString("departmentid");
		}else{
			return "0";
		}
	}
	
	/**
	 * 根据用户id获取部门名称
	 * @param userid
	 * @return departmentname
	 */
	public  String getDepName(String userid){
		if(userid.equals("") || userid.equals("1")){
			return "0";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select departmentname from HrmDepartment where id = (select subcompanyid1 from HrmResource where id="+userid+")");
		log.writeLog("getDepName---> select departmentname from HrmDepartment where id = (select subcompanyid1 from HrmResource where id="+userid+")");
		if(rs.next()){
			return rs.getString("departmentname");
		}else{
			return "null";
		}
	}

	/**
	 * 根据部门id获取部门名称
	 * @param depid
	 * @return departmentname
	 */
	public  String getDepNameById(String depid){
		if(depid.equals("") || depid.equals("1")){
			return "0";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select departmentname from HrmDepartment where id ="+depid);
		log.writeLog("getDepNameById--> select departmentname from HrmDepartment where id ="+depid);
		if(rs.next()){
			return rs.getString("departmentname");
		}else{
			return "null";
		}
	}
	
	/**
	 * 根据用户id获取分部id
	 * @param userid
	 * @return subcompanyid1
	 */
	public  String getSubid(String userid){
		if(userid.equals("") || userid.equals("1")){
			return "0";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select subcompanyid1 from hrmresource where id="+userid);
		log.writeLog("getSubid---> select subcompanyid1 from hrmresource where id="+userid);
		if(rs.next()){
			return rs.getString("subcompanyid1");
		}else{
			return "0";
		}
	}
	
	/**
	 * 根据用户id获取分部名称
	 * @param userid
	 * @return subcompanyname
	 */
	public  String getSubName(String userid){
		if(userid.equals("") || userid.equals("1")){
			return "0";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select subcompanyname from HrmSubCompany where id = (select subcompanyid1 from HrmResource where id="+userid+")");
		log.writeLog("getSubName---> select subcompanyname from HrmSubCompany where id = (select subcompanyid1 from HrmResource where id="+userid+")");
		if(rs.next()){
			return rs.getString("subcompanyname");
		}else{
			return "null";
		}
	}

	/**
	 * 根据部门id获取分部名称
	 * @param subid
	 * @return subcompanyname
	 */
	public  String getSubNameById(String subid){
		if(subid.equals("") || subid.equals("1")){
			return "0";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select subcompanyname from HrmSubCompany where id = "+subid);
		log.writeLog("getSubNameById---> select subcompanyname from HrmSubCompany where id = "+subid);
		if(rs.next()){
			return rs.getString("subcompanyname");
		}else{
			return "0";
		}
	}

	/**
	 * 根据用户id获取姓名
	 * @param userid
	 * @return lastname
	 */
	public  String getLastname(String userid){
		if(userid.equals("1")){
			return "系统管理员";
		}
		RecordSet rs = new RecordSet();
		rs.execute("select lastname from hrmresource where id="+userid);
		log.writeLog("getLastname---> select lastname from hrmresource where id = "+userid);
		if(rs.next()){
			return rs.getString("lastname");
		}else{
			return "null";
		}
	}
	
	/**
	 * 根据名获取用户id
	 * @param name
	 * @return userid
	 */
	public String getUserid(String name){
		RecordSet rs = new RecordSet();
		rs.execute("select id from hrmresource where lastname = '%"+name+"%'");
		log.writeLog("getUserid---> select id from hrmresource where lastname = '%"+name+"%'");
		if(rs.next()){
			return rs.getString("id");
		}else{
			return "0";
		}
	}
	
	/**
	 * 根据工号获取用户id
	 * @param code
	 * @return userid
	 */
	public String getUseridByCode(String code){
		RecordSet rs = new RecordSet();
		rs.execute("select id from hrmresource where workcode = '"+code+"'");
		log.writeLog("getUseridByCode---> select id from hrmresource where workcode = '"+code+"'");
		if(rs.next()){
			return rs.getString("id");
		}else{
			return "0";
		}
	}
	
	/**
	 * 生成UUID
	 * @return
	 */
	public  String getUUID(){
	    String uuid = UUID.randomUUID().toString().toLowerCase();
	    return uuid;
	}
	
	/**
	 * 转化关保系统数据中的资产来源
	 * @param name
	 * @return
	 */
	public String getObjToZcly(String name){
		if(name.equals("")){
			return "";
		}else if(name.indexOf("构建")>=0){
			return "0";
		}else  if(name.indexOf("调拨")>=0){
			return "1";
		}
		return "";
	}
	
	/**
	 * 转化关保系统资产数据中的卡片状态
	 * @param name
	 * @return
	 */
	public String getObjToKpzt(String name){
		if(name.equals("")){
			return "";
		}else if(name.indexOf("在库")>=0){
			return "0";
		}else  if(name.indexOf("在用")>=0){
			return "1";
		}else if(name.indexOf("分配中")>=0){
			return "2";
		}else  if(name.indexOf("调拨中")>=0){
			return "3";
		}else if(name.indexOf("已调拨")>=0){
			return "4";
		}else  if(name.indexOf("已差错更正")>=0){
			return "5";
		}else if(name.indexOf("已报损报废")>=0){
			return "6";
		}else  if(name.indexOf("减少中")>=0){
			return "7";
		}else if(name.indexOf("已减少")>=0){
			return "8";
		}else if(name.indexOf("报损报废中")>=0){
			return "8";
		}
		return "";
	}

	/**
	 * 检查用户是否为资产管理员
	 * @param userid
	 * @param roleid
	 * @return
	 */
	public Boolean checkIsAdmin(String userid,String roleid){
		RecordSet rs = new RecordSet();
		String sql=" select 1 from hrmrolemembers where roleid in ("+roleid+") and resourceid in ("+userid+")" +
				" union all " +
				" select 1 from hrmrolemembers a,HrmDepartment c,hrmresource d  where roleid in ("+roleid+") and a.resourcetype=3 and a.resourceid=c.id " +
				" and d.departmentid=c.id and d.id in ("+userid+")";
		log.writeLog("checkIsAdmin---> "+sql);
		rs.execute(sql);
		if(rs.next()){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 根据关保资产表id获取该条数据信息
	 * @param id
	 * @return
	 */
	public Map<String,String> getGbzcxx(String id){
		Map<String,String> data = new HashMap<String,String>();
		RecordSet rs = new RecordSet();
		String sql = "select * from uf_gbxtzczmxx where id='"+id+"'";
		rs.execute(sql);
		log.writeLog("getGbzcxx---> "+sql);
		if(rs.next()){
			data.put("id",rs.getString("id"));
			data.put("kpwybs",rs.getString("kpwybs"));
			data.put("kph",rs.getString("kph"));
			data.put("mldm",rs.getString("mldm"));
			data.put("mlmc",rs.getString("mlmc"));
			data.put("gdzcbm",rs.getString("gdzcbm"));
			data.put("gdzcmc",rs.getString("gdzcmc"));
			data.put("pp",rs.getString("pp"));
			data.put("ggxh",rs.getString("ggxh"));
			data.put("hth",rs.getString("hth"));
			data.put("dj",rs.getString("dj"));
			data.put("qdrq",rs.getString("qdrq"));
			data.put("sybm",rs.getString("sybm"));
		}
		log.writeLog("getGbzcxx："+data.toString());
		return data;
	}

	/**
	 * 根据品牌名称查询品牌表中的id值
	 * @param pp
	 * @return id
	 */
	public String getPp(String pp){
		String id = "";
		RecordSet rs = new RecordSet();
		String sql = "select * from uf_swzcpp where ppmc ='"+pp+"'";
		rs.execute(sql);
		log.writeLog("getPp---> "+sql);
		if(rs.next()) {
			return  rs.getString("id");
		}
		return id;
	}

	/**
	 * 根据规格型号查询品牌表中的id值
	 * @param ggxh
	 * @return id
	 */
	public String getGgxh(String ggxh){
		String id = "";
		RecordSet rs = new RecordSet();
		String sql = "select * from uf_swzcggxh where ppmc= '"+ggxh+"'";
		rs.execute(sql);
		log.writeLog("getGgxh---> "+sql);
		if(rs.next()) {
			return  rs.getString("id");
		}
		return id;
	}

	/**
	 * 根据建模表名和uuid查找对应的数据id
	 * @param tablename
	 * @param uuid
	 * @return
	 */
	public String getBillid(String tablename,String uuid){
		String id = "";
		RecordSet rs = new RecordSet();
		String sql = "select * from "+tablename+" where uuid='"+uuid+"'";
		rs.execute(sql);
		log.writeLog("getBillid---> "+sql);
		if(rs.next()) {
			return  rs.getString("id");
		}
		return id;
	}

	/**
	 * 表单建模权限重构方法，重构后才能在台账里查看到数据
	 * @param creater 创建人
	 * @param formmodeid 模块id
	 * @param billid 数据id,即对应uf表里的id
	 */
	public void refreshRight( int creater, int formmodeid, int billid){
		try{
			ModeRightInfo modeRightInfo = new ModeRightInfo();
			//console.info("当前重构数据：creater=" + creater + "  formmodeid=" + formmodeid + "  billid=" + billid);
			modeRightInfo.setNewRight(true);
			modeRightInfo.editModeDataShare(creater, formmodeid, billid);
			modeRightInfo.addDocShare(creater, formmodeid, billid);
		}
		catch (Exception e){
			log.writeLog("建模权限重构代码报错：creater=" + creater + "  formmodeid=" + formmodeid + "  billid=" + billid);
			log.writeLog(e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 根据关保目录代码表id获取对应的设备类型
	 * @param mldm 关保目录代码表id
	 * @return
	 */
	public String getSblxByMldm(String mldm){
		String sblx = "";
		RecordSet rs = new RecordSet();
		String sql = "select * from uf_sbfldygx where gbmldm ='"+mldm+"'";
		rs.execute(sql);
		log.writeLog("getSblxByMldm---> "+sql);
		if(rs.next()) {
			return  rs.getString("swsbfl");
		}
		return sblx;
	}

	/**
	 * 根据目录代码名称获取对应的关保资产类型浏览按钮id
	 * @param mldm
	 * @return
	 */
	public int getGbzclxByMldm(String mldm){
		int id = 0;
		RecordSet rs = new RecordSet();
		String sql = "select * from uf_gzcmldm where mldm ='"+mldm+"'";
		rs.execute(sql);
		log.writeLog("getGbzclxByMldm---> "+sql);
		if(rs.next()) {
			return  rs.getInt("id");
		}
		return  id;
	}

	public String getSubcomanyIsCancel(String subid){
		String cancel = "";
		RecordSet rs = new RecordSet();
		String sql = "select canceled from HrmSubCompany where id='"+subid+"'";
		rs.execute(sql);
		log.writeLog("getSubcomanyIsCancel---> "+sql);
		if(rs.next()) {
			return  rs.getString("canceled");
		}
		return cancel;
	}

	/**
	 * 获取部门资产管理员
	 * @param dybm
	 * @return
	 */
	public String getAssetsAdmin_bm(String dybm){
		RecordSet rs = new RecordSet();
		String assetsAdmin = "";
		String sql = "select swzcgly from Matrixtable_1 where id="+dybm;
		rs.execute(sql);
		while(rs.next()){
			assetsAdmin = rs.getString("swzcgly");
		}
		return assetsAdmin;
	}

	/**
	 * 获取科室管理员
	 * @param syks
	 * @return
	 */
	public String getAsstesAdmin_ks(String syks){

		RecordSet rs = new RecordSet();
		String assetsAdmin = "";
		String sql = "select swzcgly from Matrixtable_2 where id="+syks;
		rs.execute(sql);
		while(rs.next()){
			assetsAdmin = rs.getString("swzcgly");
		}
		return assetsAdmin;
	}
}
